package com.shop.portal.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GlobalException implements HandlerExceptionResolver {
    private static final Logger logger = LoggerFactory.getLogger(GlobalException.class);

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

        logger.info("进入portal 全局异常处理器！");

        /**
         * 控制台打印异常
         */
        ex.printStackTrace();
        logger.error("发生异常", ex);

        // 跳转错误页面
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("message", "服务器开小差，请稍后重试！");
        modelAndView.setViewName("error/exception");

        return modelAndView;

    }
}
